<?php

class UrlUtil {

    /**
     * 根据url获取请求参数
     * @param string $url
     * @return array
     */
    public static function queryParams($url) {
        $params = [];
        $arr = parse_url($url);
        parse_str($arr['query'], $params);
        return $params;
    }

    /**
     * 根据协议将url替换成https协议头
     * @param string $url       待替换url地址，必须以http开头
     * @return mixed            替换成https协议url地址
     */
    public static function http2https($url) {
        $arr = parse_url($url);
        if (!isset($arr['scheme']) || $arr['scheme'] == 'https' || $arr['scheme'] != 'http' || substr($url, 0, 4) != 'http') {
            return $url;
        }

        $url = "https" . substr($url, 4);
        return $url;
    }

    /**
     * 获取url中域名部分
     * @param $url
     * @return mixed
     */
    public static function getHost($url)
    {
        $arr = parse_url($url);
        return $arr['host'];
    }

}
